#pragma once
#include <mutex>
#include "Util.hpp"

class Config {
public:
    static Config* GetInstance() {
        if(instance_ == nullptr) {
            std::unique_lock<std::mutex> lock(mutex_);
            if(instance_ == nullptr) {
                instance_ = new Config();
            }
        }
        return instance_;
    }
    int GetPort() { return port_; }
    int GetGsize() { return gsize_; }
    int GetGblacklog() { return gbacklog_; }
    std::string GetLoggerName() { return loggger_name_; }
    std::string GetLogFilePath() { return log_file_path_; }
    std::string GetServerIp() { return server_ip_; }
private:
    Config(){
        if(ReadConfigFile() == false){
            Logger::getInstance().loggger()->fatal("配置文件初始化失败");
            exit(-1);
        }
    }
    bool ReadConfigFile() {
        FileUtil file;
        std::string body;
        Json::Value root;
        if(file.GetContent(&body) == false) { return false; }
        if(!JsonUtil::UnSerialize(body, &root)) { return false; }
        port_ = root["port"].asUInt();
        gsize_ = root["gsize"].asInt();
        loggger_name_ = root["logger_name"].asString();
        log_file_path_ = root["log_file_path"].asString();
        server_ip_ = root["server_ip"].asString();
        gbacklog_ = root["gbacklog"].asInt();
        return true;
    }
    static Config *instance_;
    static std::mutex mutex_;
private:
    int port_;//服务器端口号
    int gsize_;
    int gbacklog_;
    std::string loggger_name_;//日志器名称
    std::string log_file_path_;//日志文件的保存路径
    std::string server_ip_;//服务器ip
};
Config* Config::instance_ = nullptr;
std::mutex Config::mutex_;
